package com.example.androidtest.recycleview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidtest.R;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter {
    private List<String> mList;
    private List<String> mList2;
    private List<Integer> mList3;
    private Context mContext;

    public HomeAdapter(List<String> mList, List<String> mList2, List<Integer> mList3, Context mContext) {
        this.mList = mList;
        this.mList2 = mList2;
        this.mList3 = mList3;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public HomeAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater=LayoutInflater.from(mContext);
        View view=layoutInflater.inflate(R.layout.activity_recyclerview_item,viewGroup,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((MyViewHolder)viewHolder).tv_name.setText(mList.get(i));
        ((MyViewHolder) viewHolder).tv_content.setText(mList2.get(i));
        ((MyViewHolder) viewHolder).iv_avatar.setBackgroundResource(mList3.get(i));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        TextView tv_content;
        ImageView iv_avatar;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_content=itemView.findViewById(R.id.tv_content);
            iv_avatar=itemView.findViewById(R.id.iv_avatar);
        }
    }
}
